package com.example.makerspace.dao;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.CompetitionDO;
import com.example.makerspace.dto.PageRequest;
import com.example.makerspace.dto.Pagination;

import java.util.ArrayList;
import  java.util.List;
import com.example.makerspace.mapper.CompetitionMapper;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashSet;

@Component
public class CompetitionDAO extends BaseDAO<CompetitionDO>{
    @Resource
    private CompetitionMapper competitionMapper;

    @PostConstruct
    public void init(){
        this.mapper = competitionMapper;
    }

    public CompetitionDO selectByTitle(String title){
        return this.mapper.selectOne(new QueryWrapper<CompetitionDO>().eq("title", title));
    }

    public IPage<CompetitionDO>  presentCompetitions(PageRequest pageRequest){
        Pagination pagination = pageRequest.getPagination() ;
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<CompetitionDO>()
                .eq("competition_parent_id",pageRequest.getCompetitionParentId())
                .lt("start_time", new Date(System.currentTimeMillis()))
                .gt("end_time", new Date(System.currentTimeMillis()));
        queryWrapper.orderByDesc("create_time");
        return mapper.selectPage(new Page<>(pagination.getPageNum(), pagination.getSize()), queryWrapper);
    }

    public  IPage<CompetitionDO>  selectByCondition(Pagination pagination, String queryParam,Integer competitionParentId) {
         List<CompetitionDO> competitionDOList = new ArrayList<>();
        List<CompetitionDO> competitionDOList2 = new ArrayList<>();
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",queryParam);
        queryWrapper.eq("competition_parent_id", competitionParentId);
        Page<CompetitionDO> competitionDOTitle = mapper.selectPage(new Page<>(pagination.getPageNum(), pagination.getSize()), queryWrapper);
        queryWrapper.like("content",queryParam);
        queryWrapper.eq("competition_parent_id", competitionParentId);
        Page<CompetitionDO> competitionDOContent = mapper.selectPage(new Page<>(pagination.getPageNum(), pagination.getSize()), queryWrapper);
        competitionDOList.addAll(competitionDOTitle.getRecords());
        competitionDOList.addAll(competitionDOContent.getRecords());
        HashSet<CompetitionDO> competitionDOS = new HashSet<>(competitionDOList);
        competitionDOS.addAll(competitionDOList);
        competitionDOList2.addAll(competitionDOS);
        competitionDOTitle.setRecords(competitionDOList2);

        return  competitionDOTitle;
    }

    public IPage<CompetitionDO> selectByType(Pagination paginationint,int typeInt,int competitionParentId) {
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", typeInt).eq("competition_parent_id",competitionParentId);
        return  mapper.selectPage(new Page<>(paginationint.getPageNum(),paginationint.getSize()), queryWrapper);
    }

    public IPage<CompetitionDO> selectByCompetitionParentId(long pageNum, long size, Integer competitionParentId) {
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("competition_parent_id", competitionParentId);
        return  mapper.selectPage(new Page<>(pageNum,size), queryWrapper);
    }

    public IPage<CompetitionDO> selectByCompetitionParentId(Pagination pagination, Integer competitionParentId) {
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("competition_parent_id", competitionParentId);
        return  mapper.selectPage(new Page<>(pagination.getPageNum(),pagination.getSize()), queryWrapper);
    }

    public CompetitionDO selectByIdAndCompetitionParentId(Integer competitionId, Integer competitionParentId) {
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", competitionId).eq("competition_parent_id",competitionParentId);
        return  mapper.selectOne(queryWrapper);
    }

    public CompetitionDO selectByCompetitionId(Integer competitionId) {
        QueryWrapper<CompetitionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", competitionId);
        return mapper.selectOne(queryWrapper);
    }
}
